#!/usr/bin/python

import threading
import mutex
import time

def main():
    stdoutMutex = mutex.mutex()

    threadA = ThreadOne(stdoutMutex)
    threadA.start()

    while True:
        stdoutMutex.lock(A, mutex)

def A(mutex):
    time.sleep(1)
    print "[RUNNING] main: '%s'" % threading.current_thread().name

class ThreadOne(threading.Thread):

    def __init__(self, stdoutMutex):
        threading.Thread.__init__(self)
        self.daemon = True

        self.stdoutMutex = stdoutMutex

    def run(self):
        while True:
            self.stdoutMutex.lock(printInfo, (self.stdoutMutex, 'ThreadOne'))
            self.stdoutMutex.unlock()
            print "[TESTUNLOCKED] ThreadOne: Locked? %s" % self.stdoutMutex.test()

def printInfo(arg):
    mutex, caller = arg
    print "[LOCKED] %s: '%s'" % (caller, threading.current_thread().name)
    print "[RUNNING] ThreadOne: '%s'" % threading.current_thread().name
    print "[TESTLOCKED] ThreadOne: Locked? %s" % mutex.test()

if __name__ == '__main__':
    main()

